package orderServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "orderList",urlPatterns = "/orderList")
public class orderList extends HttpServlet {
    java.sql.Connection conn = null;
    String strConn;
    java.sql.Statement sqlStmt = null;
    ResultSet sqlRst = null;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try
        {
            int YEAR;
            String year =request.getQueryString();
            if(year!=null)
            {
                YEAR = Integer.parseInt(year);
            }
           else  YEAR = 2019 ;
            Class.forName("org.h2.Driver");
            conn = java.sql.DriverManager.getConnection("jdbc:h2:~/jpa-demo-db;MODE=MYSQL;DB_CLOSE_DELAY=-1", "sa", "sa");
            sqlStmt = conn.createStatement();
            String sql ="select * from order1 where year(date)";
            sqlRst = sqlStmt.executeQuery(sql);
            List<orderBean> list = new ArrayList<>();
            while(sqlRst.next())
            {
                String ono = sqlRst.getString("ono");
                String name = sqlRst.getString("name");
                String phone = sqlRst.getString("phone");
                String rno  = sqlRst.getString("rno");
                String de_time = sqlRst.getString("de_time");
                String  check_time = sqlRst.getString("check_time");
                int  price = sqlRst.getInt("price");
                int pledge = sqlRst.getInt("pledge");
                int count = sqlRst.getInt("count");
                orderBean ob = new orderBean(ono,name,phone,rno,de_time,check_time,price,pledge,count);
                list.add(ob);
            }
            request.getSession().setAttribute("list",list);
        }
        catch (Exception ex)
        {
            System.out.println(ex.toString());
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
